package com.ruoyi.project.rest.statistics.controller;

import java.util.*;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.project.rest.detail.domain.RestOrderDetail;
import com.ruoyi.project.rest.order.service.IRestOrderService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.aspectj.weaver.loadtime.Aj;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.rest.statistics.domain.RestStatisticsData;
import com.ruoyi.project.rest.statistics.service.IRestStatisticsDataService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;

/**
 * 统计数据Controller
 *
 * @author jfj
 * @date 2022-04-08
 */
@Controller
@RequestMapping("/rest/statistics")
public class RestStatisticsDataController extends BaseController {

    @Autowired
    private IRestStatisticsDataService restStatisticsDataService;
    @Autowired
    private IRestOrderService restOrderService;

    /**
     * 查询当天的数据
     *
     * @return
     */
    @PostMapping("/getDayData")
    @ResponseBody
    public AjaxResult getDayData() {
        RestStatisticsData restStatisticsData = new RestStatisticsData();
        Calendar cal = Calendar.getInstance();
        cal.setTime(new Date());
        cal.add(Calendar.DAY_OF_MONTH, -1);
        List<RestStatisticsData> statisticsData = restOrderService.selectStatisticsQuery(DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD, cal.getTime()), DateUtils.getDate());
        if (statisticsData != null && statisticsData.size() == 1) {
            restStatisticsData = statisticsData.get(0);
        }
        return AjaxResult.success(restStatisticsData);
    }

    @PostMapping("/getHeadData")
    @ResponseBody
    public AjaxResult getHeadData() {
        Map<String, Object> result = new HashMap<>();
        RestStatisticsData restStatisticsData = new RestStatisticsData();
        Calendar c = Calendar.getInstance();
        c.setTime(new Date());
        c.add(Calendar.DAY_OF_MONTH, -1);
        restStatisticsData.setStatisticsTime(c.getTime());
        List<RestStatisticsData> list = restStatisticsDataService.selectRestStatisticsDataList(restStatisticsData);
        if (list != null && list.size() > 0) {
            restStatisticsData = list.get(0);
        }
        result.put("yesterData", restStatisticsData);

        Calendar calMonth = Calendar.getInstance();
        calMonth.setTime(new Date());  //修改成需要的日期
        calMonth.add(Calendar.MONTH, 0);   // -1表示上个月，0表示本月，1表示下个月，上下月份以此类型
        calMonth.set(Calendar.DAY_OF_MONTH, calMonth.getActualMinimum(Calendar.DAY_OF_MONTH));
        RestStatisticsData monthData = restStatisticsDataService.selectMonthData(DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD, calMonth.getTime()), DateUtils.getDate());
        result.put("monthData", monthData);

        Calendar calYear = Calendar.getInstance();
        calYear.setTime(new Date());  //修改成需要的日期
        calYear.set(Calendar.MONTH, 1);   // -1表示上个月，0表示本月，1表示下个月，上下月份以此类型
        calYear.set(Calendar.DAY_OF_MONTH, calMonth.getActualMinimum(Calendar.DAY_OF_MONTH));
        RestStatisticsData yearData = restStatisticsDataService.selectMonthData(DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD, calYear.getTime()), DateUtils.getDate());
        result.put("yearData", yearData);
        return AjaxResult.success(result);
    }

    @PostMapping("/getMonthListData")
    @ResponseBody
    public AjaxResult getMonthListData() {
        Map<String, Object> result = new HashMap<>();
        Calendar calMonth = Calendar.getInstance();
        calMonth.setTime(new Date());  //修改成需要的日期
        calMonth.add(Calendar.MONTH, 0);   // -1表示上个月，0表示本月，1表示下个月，上下月份以此类型
        calMonth.set(Calendar.DAY_OF_MONTH, calMonth.getActualMinimum(Calendar.DAY_OF_MONTH));
        List<RestStatisticsData> monthList = restStatisticsDataService.selectDataList(DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD, calMonth.getTime()), DateUtils.getDate(), 1);
        result.put("monthList", monthList);

        Calendar calYear = Calendar.getInstance();
        calYear.setTime(new Date());  //修改成需要的日期
        calYear.set(Calendar.MONTH, 1);   // -1表示上个月，0表示本月，1表示下个月，上下月份以此类型
        calYear.set(Calendar.DAY_OF_MONTH, calMonth.getActualMinimum(Calendar.DAY_OF_MONTH));
        List<RestStatisticsData> yaerList = restStatisticsDataService.selectDataList(DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD, calYear.getTime()), DateUtils.getDate(), 2);
        result.put("yaerList", yaerList);
        return AjaxResult.success(result);
    }

    @PostMapping("/getRecentData")
    @ResponseBody
    public AjaxResult getRecentData() {
        return restStatisticsDataService.getRecentData();
    }


}
